﻿using System;
using NUnit.Framework;
using OpenQA.Selenium;
using System.Collections.Generic;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class D011_NewUserMultiPost {
		private const Int32 intervalQuarantineTest = 1;
		private const String password = "123456";

		private IWebDriver driver;
		private String username;
		private String email;
		private List<TopicContainer> elements = new List<TopicContainer> {
												new TopicContainer { Title = String.Format("Multi-post enabled, post: {0}", 1), Content =  "bla bla, should show up!", ShouldBeVisible = true },
												new TopicContainer { Title = String.Format("Multi-post enabled, post: {0}", 2), Content =  "bla bla, should show up!", ShouldBeVisible = true },
												new TopicContainer { Title = String.Format("Multi-post enabled, post: {0} should be quarantined", 3), Content =  "NEW user multi post bla bla, should end up in Quarantine!", ShouldBeVisible = false }
											};

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public void TestInitialize() {
			driver = TestHelper.GetDriver();

			username = Guid.NewGuid().ToString();
			email = String.Format("{0}@mvcforum.org", username);
			driver.CreateUser(username, password, email);

			// Log on as administrator!
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			// Enable multi-post anti-spam!
			driver.EnableMultiPost(intervalQuarantineTest, elements.Count - 1);
			// Disable new user trial
			driver.DisableNewUserTrial();

			driver.LogOff();

		}

		[TestFixtureTearDown]
		public void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		[Test]
		public void MultiPostBlock() {
			driver.LogOn(email, password);

			String title = String.Empty;
			String content = String.Empty;
			foreach (TopicContainer con in elements) {
				title = String.Format("{0} {1}", con.Title, DateTime.UtcNow);
				driver.CreateTopic("Forum 1", title, con.Content);

				Assert.AreEqual(String.Format("{0}forum/viewforum/forum-1/1", TestConfiguration.SiteBaseURL), driver.Url.ToLower());
				Assert.AreEqual(con.ShouldBeVisible, driver.PageSource.Contains(title), con.ShouldBeVisible ? String.Format("The topic '{0}' should be visible, but isn't!", con.Title) : "The topic shouldn't be visible, but is!");
			}

			// Let's wait the interval and try again!
			driver.Wait(60 * (intervalQuarantineTest + 1));

			title = String.Format("Multi-post enabled, post: {1} {0}", DateTime.UtcNow, elements.Count);
			content = "bla bla, should show up! We're past the interval!";
			driver.CreateTopic("Forum 1", title, content);

			Assert.AreEqual(String.Format("{0}forum/viewforum/forum-1/1", TestConfiguration.SiteBaseURL), driver.Url.ToLower());
			Assert.AreEqual(true, driver.PageSource.Contains(title), "The topic should be visible, but isn't");
		}
	}
}